package com.roocon.design_pattern.chapter02_observer.demo1;

import java.util.ArrayList;
import java.util.List;

/**
 * 事件源
 */
public class Subject {
  private List<Observer> observers = new ArrayList<>();

  private int state;

  public void setState(int state) {
    if (state != this.state) {
      this.state = state;
      notifyAllObservers();
    }
  }

  public void attach(Observer observer) {
    observers.add(observer);
  }

  public void notifyAllObservers() {
    observers.forEach(Observer::update);
  }
}
